import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [{
      path: 'home',
      name: 'Home',
      component: () => import('@/views/home/index'),
      meta: { title: '主页', icon: 'dashboard' }
    }]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/task',
    component: Layout,
    redirect: '/task/index',
    name: 'Task',
    meta: { title: '任务', icon: 'el-icon-s-help', roles: ['admin'] },
    children: [
      {
        path: 'index',
        name: 'View',
        component: () => import('@/views/task/index'),
        meta: { title: '任务一览', icon: 'table' }
      },
      {
        path: 'publish',
        name: 'Publish',
        component: () => import('@/views/task/publish'),
        meta: { title: '发布任务', icon: 'tree' }
      },
      {
        path: 'edit',
        name: 'TaskEdit',
        component: () => import('@/views/task/edit'),
        hidden: true,
        meta: { title: '编辑', icon: 'table', noCache: true }
      },
      {
        path: 'status',
        name: 'TaskStatus',
        component: () => import('@/views/task/status'),
        hidden: true,
        meta: { title: '任务进度' }
      }
    ]
  },
  {
    path: '/teacher',
    component: Layout,
    redirect: '/teacher/task',
    name: 'Teacher',
    meta: {
      title: '任务',
      icon: 'el-icon-s-help',
      roles: ['editor']
    },
    children: [
      {
        path: 'task',
        name: 'MyTaskList',
        component: () => import('@/views/teacher/task'),
        meta: { title: '任务列表', icon: 'table', noCache: true }
      },
      {
        path: 'index',
        name: 'MyTask',
        hidden: true,
        component: () => import('@/views/teacher/index'),
        meta: { title: '查看任务', icon: 'table', noCache: true }
      },
      {
        path: 'edit',
        name: 'EditData',
        component: () => import('@/views/teacher/edit'),
        hidden: true,
        meta: { title: '编辑', icon: 'table', noCache: true }
      }
    ]
  },

  {
    path: '/account',
    redirect: '/account/index',
    component: Layout,
    meta: { roles: ['admin'] },
    children: [
      {
        path: 'index',
        name: 'userList',
        component: () => import('@/views/account/index'),
        meta: { title: '账号管理', icon: 'form' }
      },
      {
        path: 'add',
        name: 'addUser',
        component: () => import('@/views/account/add'),
        hidden: true
      }
    ]
  },
  {
    path: '/measure',
    redirect: '/measure/index',
    component: Layout,
    name: 'Measure',
    meta: {
      title: '体测成绩',
      icon: 'form',
      roles: ['admin']
    },
    children: [
      {
        path: 'index',
        name: 'MeasureData',
        component: () => import('@/views/measure/index'),
        meta: { title: '体测数据', icon: 'form' }
      },
      // {
      //   path: 'download',
      //   name: 'ScoreDownload',
      //   component: () => import('@/views/measure/download'),
      //   meta: { title: '成绩下载', icon: 'form' }
      // },
      {
        path: 'detail',
        name: 'StudentDetail',
        hidden: true,
        component: () => import('@/views/measure/detail'),
        meta: { title: '详情查看', icon: 'form' }
      }
    ]
  },
  // {
  //   path: '/analyse',
  //   redirect: '/analyse/index',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'index',
  //       name: 'Analyse',
  //       component: () => import('@/views/analyse/index'),
  //       meta: { title: '总体分析', icon: 'form', roles: ['admin'] }
  //     }
  //   ]
  // },

  // {
  //   path: '/handle',
  //   component: Layout,
  //   redirect: '/handle/index',
  //   name: 'Nested',
  //   meta: {
  //     title: '上报数据处理',
  //     icon: 'nested',
  //     roles: ['admin']
  //   },
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/handle/index'), // Parent router-view
  //       name: 'ReportView',
  //       meta: { title: '查看情况' }
  //     },
  //     {
  //       path: 'download',
  //       component: () => import('@/views/handle/download'),
  //       name: 'Download',
  //       meta: { title: '处理结果' }
  //     }
  //   ]
  // },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
